package com.nd.qa.shareplate.jdom;

import java.io.IOException;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class CreateXmlFileDemo {

	public static void main(String[] args) {
		//SAX是基于事件流的解析,DOM是基于XML文档树结构的解析
        try {
        	//root element
    		Element classElement = new Element("cars");
    		Document document = new Document(classElement);
    		
    		//supercars element
    		Element supercarElement = new Element("supercars");
    		supercarElement.setAttribute("company","Ferrari");
    		
    		//supercars element
            Element carElement1 = new Element("carname");
            carElement1.setAttribute("type","formula one");
            carElement1.setText("Ferrari 101");

            Element carElement2 = new Element("carname");
            carElement2.setAttribute("type","sports");
            carElement2.setText("Ferrari 202");
            
            supercarElement.addContent(carElement1);
            supercarElement.addContent(carElement2);
            document.getRootElement().addContent(supercarElement);
            
            //吸入
            XMLOutputter xmlOutputter = new XMLOutputter();
            xmlOutputter.setFormat(Format.getPrettyFormat());
			xmlOutputter.output(document, System.out);
			xmlOutputter.clone();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
